<?php
declare(strict_types=1);

namespace App\Controller;

use App\Service\DyPayService;
use App\Service\DyService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Service\RewardService;
use App\Service\RechargeService;
use App\Service\UserService;

#[AutoController]
class RechargeController extends BaseController
{
    #[Inject]
    private RewardService $rewardService;
    #[Inject]
    private RechargeService $rechargeService;
    #[Inject]
    private DyPayService $dypayService;
    #[Inject]
    private UserService $userService;
    #[Inject]
    private DyService $dyService;

    public function goos_list(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list_data = $this->rechargeService->get_user_goods_list($applet_id,$user_id);
        return $this->resultResponse(["data"=>$list_data]);
    }
    public function order_back(RequestInterface $request,ResponseInterface $response){
        $order_id = $request->input('order_id', '');
        if($order_id) {
            $res = $this->rechargeService->order_back($order_id);
            if($res == true) {
                return $this->resultResponse(["data"=>$res]);
            } else {
                return $this->resultResponse(["code"=>20004]);
            }
        } else {
            return $this->resultResponse(["code"=>20003]);
        }
    }

    public function buy(RequestInterface $request,ResponseInterface $response){
        # 下单
        $applet_id = $this->get_applet_id();
        $playlet_id = $request->input('playlet_id', 0);
        $platform = $request->input('platform', 'ANDROID');
        $isIm = $request->input('is_im', 0);

        $headers = $request->getHeaders();
        $applet_version = '1.0.1';
        if(!empty($headers['applet_version']) && !empty($headers['applet_version'][0])){
            $applet_version = $headers['applet_version'][0];
        }

        $xc_open_id = $request->input('xc_open_id', '');
        $goods_id = $request->input('recharge_goods_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($goods_id)){
            return $this->resultResponse(["code"=>20050]);
        }
        $applet_info =  $this->rechargeService->get_applet($applet_id);

        $isIos = strtoupper($platform) == 'IOS';
        $user_platform = $isIos ? 2 : 1;
        if((!!$isIm && $isIos) || ($applet_version < '1.0.5' && $isIos)){
            $isIm = true;
        }else{
            $isIm = false;
        }

        #生成订单
        $list_data = $this->rechargeService->buy($applet_id,$user_id,$goods_id,$playlet_id,1, $user_platform);
        $data = [];
        $data['applet_id'] = $applet_id;
        $data['order_id'] = $list_data['order_id'];
        $data['amount'] = $list_data['amount'];
        $goods_list = [];
        $skuList = [
            'skuId'=>$goods_id,
            'price'=>$list_data['amount'],
            'quantity'=>1,
            'title'=>'短剧充值',
            'type'=>401,
            'tagGroupId'=>'tag_group_7272625659888041996',
//            'entrySchema'=>"{path:'page/path/index'}",
            'entrySchema'=>['path'=>'page/path/index'],
            'skuId'=>'短剧充值',
            'imageList'=>['https://ky-dj.oss-accelerate.aliyuncs.com/images/aa/aaa682b510876c1119e451fb31ba6a28.jpg'],
        ];
        $goods_list['skuList'] = [$skuList];
        $goods_list['payNotifyUrl'] = 'https://kydy.kuaiyingbox.com/dy_pay/check_signature';
        $goods_list['outOrderNo'] = $list_data['order_id'];
        $goods_list['totalAmount'] = $list_data['amount'];
        $goods_list['orderEntrySchema'] = ['path'=>'page/path/index'];

        if(!empty($isIm)){
            $goods_list['payScene'] = 'IM';
        }

        $goods_list = json_encode($goods_list);
        $sign =  $this->dypayService->getOrderSign($goods_list,$applet_info['app_id']);
        $byteAuthorization = $sign;
        $returndata ['data'] = $goods_list;
        $returndata ['order_id'] = $list_data['order_id'];
        $returndata ['byteAuthorization'] = $byteAuthorization;

//        #担保支付 预下单
//        $res = $this->dypayService->ttpay($data);
//        var_dump($res);
//        if($res['err_no'] == 0) {
//            $this->dyService->syn_order($applet_id,$xc_open_id,$data['order_id'],$data['amount'],0);
//            $orderInfo['order_id'] = $res['data']['order_id'];
//            $orderInfo['id'] = $list_data['order_id'];
//            $orderInfo['order_token'] = $res['data']['order_token'];
//            var_dump($orderInfo);
//
//            return $this->resultResponse(["data"=>$orderInfo]);
//        } else {
//            return $this->resultResponse(['code'=>20062]);
//        }
        return $this->resultResponse(["data"=>$returndata]);
    }
    public function dy_check_order(RequestInterface $request,ResponseInterface $response){
        $order_id = $request->input('order_id', '');
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($order_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $res = $this->rechargeService->dy_check_order($order_id,$xc_open_id);
        return $this->resultResponse(["data"=>$res]);
    }
    public function buy_ios(RequestInterface $request,ResponseInterface $response){
        # 下单
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        $goods_id = $request->input('recharge_goods_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($goods_id)){
            return $this->resultResponse(["code"=>20050]);
        }

        $list_data = $this->rechargeService->buy_ios($applet_id,$user_id,$goods_id);
        return $this->resultResponse(["data"=>$list_data]);
    }
    public function gei_sign(RequestInterface $request,ResponseInterface $response){
        $data = $request->input('signData');
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        $data = json_decode($data,true);
        $list_data = $this->rechargeService->get_sign($applet_id,$xc_open_id,$data);
//        var_dump($list_data);
        return $this->resultResponse(["data"=>$list_data]);
    }





}